package com.qf.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.entity.TbAssignmentbook;
import com.qf.service.TbAssignmentbookService;
import com.qf.mapper.TbAssignmentbookMapper;
import com.qf.utils.Result;
import com.qf.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

/**
 *
 */
@Service
public class TbAssignmentbookServiceImpl extends ServiceImpl<TbAssignmentbookMapper, TbAssignmentbook>
    implements TbAssignmentbookService{
    private final TbAssignmentbookMapper tbAssignmentbookMapper;

    public TbAssignmentbookServiceImpl(TbAssignmentbookMapper tbAssignmentbookMapper) {
        this.tbAssignmentbookMapper = tbAssignmentbookMapper;
    }

    @Override
    public Result sendbook(MultipartFile file, String title, String background, String work, Long studentId, HttpSession session) {

        String fileName = file.getOriginalFilename();
        String ext = fileName.substring(fileName.lastIndexOf("." + 1));
        String firstName = UUIDUtils.getUUID();
        fileName=firstName+"."+ext;
        String realPath = session.getServletContext().getRealPath("/file");
        File file1=new File(realPath+ File.separator+fileName);
        if(!file1.exists()){
            file1.mkdirs();
        }
        try {
            file.transferTo(file1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        TbAssignmentbook assignmentbook=new TbAssignmentbook();
        assignmentbook.setTitle(title);
        assignmentbook.setBackground(background);
        assignmentbook.setAfile(fileName);
        assignmentbook.setStudentId(Integer.parseInt(studentId.toString()));
        assignmentbook.setWork(work);
        tbAssignmentbookMapper.insert(assignmentbook);
        return Result.success("上传成功");
    }
}




